package com.sright.bcard.enterprise.web.controller;

import com.sright.bcard.common.service.share.dto.LoginUser;
import com.sright.bcard.content.share.service.UserService;
import com.sright.bcard.common.service.share.mvcresolver.EnableLoginAuth;
import com.sright.bcard.user.share.request.AnalysisListParam;
import com.sright.bcard.user.share.service.VisitorService;
import com.sright.common.response.SrightResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author peter
 * @description
 * @date 2020/6/3
 */
@RestController
@RequestMapping("/index")
public class IndexController extends BaseController{

    @Autowired
    private UserService userService;

    @Autowired
    private VisitorService visitorService;

    /**
     * 名片开通情况
     * @return
     */
    @GetMapping("/openingData.json")
    @EnableLoginAuth
    public SrightResponse getOpeningData() {
        return userService.getJoinCount(buildLoginUser());
    }

    /**
     * 访客排名
     * @return
     */
    @GetMapping("/visitorRanking.json")
    @EnableLoginAuth
    public SrightResponse getVisitorRanking() {
        return visitorService.getVisitorRanking(buildLoginUser());
    }

    /**
     * 访问概况
     * @return
     */
    @GetMapping("/visitorOverview.json")
    @EnableLoginAuth
    public SrightResponse getVisitorOverview() {
        return visitorService.getVisitorOverview(buildLoginUser());
    }

    /**
     * 访客分析 - 上级人脉
     * @param id
     * @return
     */
    @GetMapping("/parentRelationship.json")
    @EnableLoginAuth
    public SrightResponse parentRelationship(@RequestParam(required = false) Long id) {
        return visitorService.parentRelationship(id);
    }

    /**
     * 访客分析 - 下级人脉
     * @param id
     * @return
     */
    @GetMapping("/childrenRelationship.json")
    @EnableLoginAuth
    public SrightResponse childrenRelationship(@RequestParam(required = false) Long id) {
        return visitorService.childrenRelationship(id);
    }

    /**
     * 访客列表
     * @param analysisListParam
     * @return
     */
    @GetMapping("/visitorAnalysis.json")
    @EnableLoginAuth
    public SrightResponse visitorAnalysisList(AnalysisListParam analysisListParam) {
        buildAnalysisList(analysisListParam);
        return visitorService.visitorAnalysisList(analysisListParam);
    }

    private LoginUser buildLoginUser() {
        LoginUser loginUser = new LoginUser();
        loginUser.setUserId(userContext.getUserId());
        loginUser.setToken(userContext.getToken());
        loginUser.setEnterpriseId(userContext.getEnterpriseId());
        return loginUser;
    }

    private void buildAnalysisList(AnalysisListParam analysisListParam) {
        analysisListParam.setUserId(userContext.getUserId());
        analysisListParam.setToken(userContext.getToken());
        analysisListParam.setEnterpriseId(userContext.getEnterpriseId());
    }
}
